App.ArticleView = Ember.View.extend({

    didInsertElement: function () {
        if ($('.ui-page-active').length) {
            $('.ui-page-active').parent().trigger('create');
        }
 
        pullDownEl = document.getElementById('pullDown');
        pullDownOffset = pullDownEl.offsetHeight;
        pullUpEl = document.getElementById('pullUp'); 
        pullUpOffset = pullUpEl.offsetHeight;
        generatedCount = 0;
        modulnya = "article";
        
        myScroll = new iScroll('wrapper', {
          useTransition: true,
          topOffset: pullDownOffset,
          onRefresh: function () {
            if (pullDownEl.className.match('loading')) {
              pullDownEl.className = '';
              pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...';
            } else if (pullUpEl.className.match('loading')) {
              pullUpEl.className = '';
              pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
            }
          },
          onScrollMove: function () {
            if (this.y > 5 && !pullDownEl.className.match('flip')) {
              pullDownEl.className = 'flip';
              pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Release to refresh...';
              this.minScrollY = 0;
            } else if (this.y < 5 && pullDownEl.className.match('flip')) {
              pullDownEl.className = '';
              pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...';
              this.minScrollY = -pullDownOffset;
            } else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
              pullUpEl.className = 'flip';
              pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Release to refresh...';
              this.maxScrollY = this.maxScrollY;
            } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
              pullUpEl.className = '';
              pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
              this.maxScrollY = pullUpOffset;
            }
          },
          onScrollEnd: function () {
            if (pullDownEl.className.match('flip')) {
              pullDownEl.className = 'loading';
              pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Loading...';        
              pullDownAction(); // Execute custom function (ajax call?)
            } else if (pullUpEl.className.match('flip')) {
              pullUpEl.className = 'loading';
              pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Loading...';        
              pullUpAction(); // Execute custom function (ajax call?)
            }
          }
        });
        
        setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);


        function pullDownAction () {

          
          var paging =  eval($('#paging').val()) + eval(1);
          $('#paging').val(paging);

           var el, li, i;
            el = document.getElementById('thelist');
          
          //load new data with ajax 

          $.ajax({ 
        
          url: "http://localhost/wnsgbl-repo/askdoctor/articles_json_page" ,
           
             success: function (data) {     
                  

                 setTimeout(function () {  // <-- Simulate network congestion, remove setTimeout from production!
                        var el, li, i;
                        el = document.getElementById('thelist');

                        var jsonData = JSON.parse(data);  
                        
                    
                        optmin = "";

                         $.each(jsonData.articles, function(i, v) {

                          var datanya = jsonData.articles[i];

                          li = document.createElement('li');
                          li.innerHTML = '<a href = "#" >'+datanya.postTitle+ '</a>';
                          el.insertBefore(li, el.childNodes[0]);
                         });

                        $("#thelist").listview("refresh");
             
                        myScroll.refresh();   // Remember to refresh when contents are loaded (ie: on ajax completion)
                      }, 1000); // <-- Simulate network congestion, remove setTimeout from production!
                               
                  } 
              });
           
           //end load new data with ajax 
 
        }

        function pullUpAction () {


          var paging =  eval($('#paging').val())+eval(1);
          $('#paging').val(paging);

          //load new data with ajax 

          $.ajax({ 
        
          url: "http://localhost/wnsgbl-repo/askdoctor/articles_json_page" ,
           
             success: function (data) {     
                  

                 setTimeout(function () {  // <-- Simulate network congestion, remove setTimeout from production!
                        var el, li, i;
                        el = document.getElementById('thelist');

                        var jsonData = JSON.parse(data);  
                        
                    
                        optmin = "";

                         $.each(jsonData.articles, function(i, v) {

                          var datanya = jsonData.articles[i];

                          li = document.createElement('li');
                          li.innerHTML = '<a href = "#" >'+datanya.postTitle+ '</a>';
                          el.appendChild(li, el.childNodes[0]);
                         });

                        $("#thelist").listview("refresh");
             
                        myScroll.refresh();   // Remember to refresh when contents are loaded (ie: on ajax completion)
                      }, 1000); // <-- Simulate network congestion, remove setTimeout from production!
                               
                  } 
              });
           
           //end load new data with ajax 
           
        } 


    }
 
});

App.ClickableView = Ember.View.extend({

    
  click: function(evt) {
    var a = "asdasd";
    alert(a);
  } 
  


});

